package com.ztjy.repository.controller;

import com.github.pagehelper.PageInfo;
import com.ztjy.bean.*;
import com.ztjy.market.biz.CustomBiz;
import com.ztjy.market.biz.OrderdetailsBiz;
import com.ztjy.market.biz.OrdersBiz;
import com.ztjy.purchase.biz.CProductBiz;
import com.ztjy.purchase.biz.CTypeBiz;
import com.ztjy.repository.biz.CkWarehouseBiz;
import com.ztjy.repository.biz.KcWarehouseBiz;
import com.ztjy.repository.biz.WarehouseBiz;
import com.ztjy.sys.biz.UsersBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.ResponseBody;
//import sun.security.provider.MD2;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/rep/ckware")
public class CkWarehouseController {
    @Autowired
    private CkWarehouseBiz ckbiz;
    @Autowired
    private CustomBiz cbiz;
    @Autowired
    private WarehouseBiz wbiz;
    @Autowired
    private UsersBiz ubiz;
    @Autowired
    private OrdersBiz obiz;
    @Autowired
    private OrderdetailsBiz ordbiz;
    @Autowired
    private KcWarehouseBiz kcwbiz;
    @RequestMapping("/allckware")
    public String allCkWare(Model model, String wareName, String indent, String state, @RequestParam(value = "pageno", defaultValue = "1") Integer pageNo, HttpSession session){
        Integer pageSize=6;
        Users user=(Users) session.getAttribute("currUser");
        Integer jobId = user.getJobId();
        Map map=new HashMap();
        map.put("1","未发货");
        map.put("2","已发货");
        map.put("3","已回款");
        map.put("4","取消订单");
        map.put("5","已退货");
        if (jobId==6||jobId==1){
            Warehouse w=new Warehouse();
            w.setCName(wareName);
            CkWarehouse ck=new CkWarehouse();
            ck.setIndent(indent);
            ck.setState(state);
            ck.setWarehouse(w);
            PageInfo<CkWarehouse> allCk = ckbiz.findAllCk(ck, pageNo, pageSize);
            model.addAttribute("wareName",wareName);
            model.addAttribute("indent",indent);
            model.addAttribute("state",state);
            model.addAttribute("ckinfo",allCk);
            model.addAttribute("map",map);
            return "forward:/storage/delivery/deliveryList.html";
        }else if(jobId==7||jobId==8){
            Warehouse w=new Warehouse();
            w.setCName(wareName);
            CkWarehouse ck=new CkWarehouse();
            ck.setIndent(indent);
            ck.setState(state);
            ck.setUserId(user.getuId());
            ck.setWarehouse(w);
            PageInfo<CkWarehouse> allCk = ckbiz.findAllCk(ck, pageNo, pageSize);
            model.addAttribute("wareName",wareName);
            model.addAttribute("indent",indent);
            model.addAttribute("state",state);
            model.addAttribute("ckinfo",allCk);
            model.addAttribute("map",map);
            return "forward:/storage/delivery/deliveryList.html";
        }else{
            return "";
        }
    }
    @RequestMapping("/detail/{id}")
    public String findCkWareDetail(Model model,@PathVariable("id") Integer id){
        CkWarehouse rep=ckbiz.findCkWarehouseById(id);
//        Orders order = obiz.findOrderById(rep.getOrders().getOrderid());
        List<Orderdetails> orderDs = ordbiz.findByOrderId(rep.getOrders().getOrderid());
        model.addAttribute("ck",rep);

        Warehouse ware=wbiz.findRepById(rep.getWarehouseId());
        model.addAttribute("ware",ware);

        Users user=ubiz.detailsUsers(rep.getUserId());
        model.addAttribute("user",user);


        Custom cum=cbiz.findByIdCustom(rep.getOrders().getCustom().getCustomid());

        model.addAttribute("cum",cum);

        Orders orders=obiz.findOrderById(rep.getIndent());
        model.addAttribute("order",orders);
//        Orderdetails orderDs=ordbiz.findOrderdetailsById(detailsId);
        model.addAttribute("orderDs",orderDs);
//        CProduct cp=cpbiz.findByIdProduct();
//        List<CDetails> cDetails =ordbiz.findOrderdetailsById(rep.getOrders().getOrderid());
          return "forward:/storage/delivery/deliveryView.html";
    }
    @RequestMapping("/goAdd")
    public String goAdd(Model model,Orders orders,Warehouse warehouse){
        orders.setStatus("2");
        warehouse.setState("1");
        List<Orders> ordersList  = obiz.findAllOrders(orders,1,99999999,null,null,null,null).getList();
        List<Warehouse> warehouses=wbiz.findAllRep(warehouse,1,9999).getList();
        model.addAttribute("ordersList",ordersList);
        model.addAttribute("warehouses",warehouses);
        return "forward:/storage/delivery/deliveryAdd.html";
    }
    @RequestMapping("/addCkware")
    public String addCkware(CkWarehouse ckWarehouse,Model model,HttpSession session){
       ckWarehouse.setState("1");
//        ckWarehouse.setCDate(new Date(System.currentTimeMillis()));

        boolean flag=ckbiz.addKcWarehouse(ckWarehouse);
        if (flag){
            return "redirect:/rep/ckware/allckware";
        }else{
            return "";
        }
    }
    @RequestMapping("/editStatus")
    public String editStatus(Integer ckwid,Integer status,HttpSession session){
        CkWarehouse ckw=ckbiz.findCkWarehouseById(ckwid);
        Orders order=ckw.getOrders();
        Warehouse warehouse=ckw.getWarehouse();
        Double oMoney=order.getOrdermoney();
        Double wMoney=warehouse.getWMoney();
        List<Orderdetails> orderDs;
        switch(status){
            case 1://发货
                ckw.setState("2");
                order.setDdState(1);
                ubiz.detailsUsers(ckwid);
                Users users= (Users) session.getAttribute("currUser");
                ckw.setUserId(users.getuId());
                ckw.setCDate(new Date(System.currentTimeMillis()));
                warehouse.setWMoney(wMoney+oMoney);
                obiz.editOrders(order);
                orderDs=ordbiz.findByOrderId(order.getOrderid());
                for (Orderdetails od:orderDs){
                    kcwbiz.updateKCW2(od.getDetailsId(),od.getPurchaseNum(),1);
                }
                break;
            case 2://取消订单
                if (ckw.getState().equals("3")){
                    warehouse.setWMoney(wMoney-oMoney);
                }
                ckw.setState("4");
                break;
            case 3://确认回款
                ckw.setState("3");
                warehouse.setWMoney(wMoney-oMoney);
                break;
            case 4://确认退货
                ckw.setState("5");
                order.setDdState(2);
                obiz.editOrders(order);
                orderDs=ordbiz.findByOrderId(order.getOrderid());
                for (Orderdetails od:orderDs) {
                    kcwbiz.updateKCW2(od.getDetailsId(), od.getPurchaseNum(), 2);
                }
                break;
        }
        boolean flag=false;
        flag=ckbiz.updateCk(ckw);
        boolean aa=flag;
        flag=wbiz.updateWare(warehouse);
        flag=aa;
        if (flag){
            return "redirect:/rep/ckware/allckware";
        }else{
            return "";
        }
    }

}
